﻿Imports System.Text
Imports System.Collections
Imports System.Threading

Namespace elevator
	Friend Class Elevator
		Private currentFloor As Integer
		Private Xcoord As Integer
		Private Ycoord As Integer
		Private doorStatus As String = "closed"
		Private movingDirection As String = "stationary"
		Private p As Panel
		Private liftUIp As Panel
		Private occupied As Boolean = False

		Private doorAnimationImages As List(Of Image)
		Private doorCloseAnimationImages As List(Of Image)

		Private commands As ArrayList

		Public Sub New(ByVal panel As Panel, ByVal luip As Panel)
			currentFloor = 1
			p = panel
			liftUIp = luip

			Xcoord = panel.Location.X
			Ycoord = panel.Location.Y

			commands = New ArrayList()

			doorAnimationImages = New List(Of Image)()
			doorCloseAnimationImages = New List(Of Image)()
			addImagesToAnimationList()
		End Sub

		Public Sub addImagesToAnimationList()
			doorAnimationImages.Add(My.Resources.closed)
			doorAnimationImages.Add(My.Resources._01)
			doorAnimationImages.Add(My.Resources._02)
			doorAnimationImages.Add(My.Resources._03)
			doorAnimationImages.Add(My.Resources._04)
			doorAnimationImages.Add(My.Resources._05)
			doorAnimationImages.Add(My.Resources._06)
			doorAnimationImages.Add(My.Resources._07)
			doorAnimationImages.Add(My.Resources._08)
			doorAnimationImages.Add(My.Resources._09)
			doorAnimationImages.Add(My.Resources._10)
			doorAnimationImages.Add(My.Resources._11)
			doorAnimationImages.Add(My.Resources._012)
			doorAnimationImages.Add(My.Resources._13)
			doorAnimationImages.Add(My.Resources._14)
			doorAnimationImages.Add(My.Resources._15)
			doorAnimationImages.Add(My.Resources._16)
			doorAnimationImages.Add(My.Resources._17)
			doorAnimationImages.Add(My.Resources._18)
			doorAnimationImages.Add(My.Resources._19)
			doorAnimationImages.Add(My.Resources._20)
			doorAnimationImages.Add(My.Resources._21)
			doorAnimationImages.Add(My.Resources._22)
			doorAnimationImages.Add(My.Resources.opened)


			For Each x As Image In doorAnimationImages
				doorCloseAnimationImages.Add(x)
			Next x

			doorCloseAnimationImages.Reverse()

		End Sub

		Public Sub addNewCommand(ByVal command As String)
			If Not(commands.Contains(command)) Then
				commands.Add(command)
			End If
		End Sub

		Private Sub emptyCommands()
			commands.Clear()
		End Sub

		Public Function getPanel() As Panel
			Return p
		End Function

		Public Sub removeCommand(ByVal command As String)
			commands.Remove(command)
		End Sub

		Public Function getNextCommand() As String
			If commands.Count <> 0 Then
				Return commands(0).ToString()
			Else
				Return ""
			End If
		End Function

		Public Function getCurrentFloor() As Integer
			Return currentFloor
		End Function

		Public Sub setCurrentFloor(ByVal floor As Integer)
			currentFloor = floor
		End Sub

		Public Sub setDoors(ByVal status As String)
			doorStatus = status
		End Sub

		Public Function getDoorStatus() As String
			Return doorStatus
		End Function

		Public Sub setMovingDirection(ByVal status As String)
			movingDirection = status
		End Sub

		Public Function getMovingDirection() As String
			Return movingDirection
		End Function

		Public Function isOccupied() As Boolean
			Return occupied
		End Function

		Public Sub setOccupied(ByVal condition As Boolean)
			occupied = condition
		End Sub


		' Reset the elevator to 0,0 when GUI parameters are changed 
		Public Sub reset(ByVal X As Integer, ByVal Y As Integer)
			setX(X)
			setY(Y)
			setCurrentFloor(1)
			setDoors("closed")
			emptyCommands()
			p.BackgroundImage = doorAnimationImages(0)
		End Sub


		' Move lift animation 
		Public Sub moveLift(ByVal direction As String)
			If direction.Equals("up") Then
				Dim animationThread As New Thread(New ThreadStart(AddressOf moveUp))
				animationThread.Start()
			ElseIf direction.Equals("down") Then
				Dim animationThread As New Thread(New ThreadStart(AddressOf moveDown))
				animationThread.Start()
			End If
		End Sub
		Private Sub moveUp()
			'if (getDoorStatus().Equals("open"))
			'{
			'    closeDoors();
			'}
			Thread.Sleep(20) ' THIS IS IMPORTANT - IT NEEDS 20ms TO CHANGE STATUS OTHERWISE IT WILL SKIP THE REST OF THE LOOP
			Do While getDoorStatus().Equals("closing")
				' Bide your time elevator
			Loop
			'int i = 0;
			'while (getDoorStatus().Equals("closed"))
			'{
				'while (i <= 300)
				'{
					Thread.Sleep(10)
					Me.setY(Ycoord - 1)
					'i++;
				'}
				'if (i == 301)
				'{
				  '  openDoors();
				'}
			'}
		End Sub
		Private Sub moveDown()
		   If getDoorStatus().Equals("open") Then
				closeDoors()
		   End If
			Thread.Sleep(20) ' THIS IS IMPORTANT - IT NEEDS 20ms TO CHANGE STATUS OTHERWISE IT WILL SKIP THE REST OF THE LOOP
			Do While getDoorStatus().Equals("closing")
				' Bide your time elevator
			Loop
			'int i = 0;
			'while (getDoorStatus().Equals("closed"))
			'{
			'    while (i <= 300)
			'    {
					Thread.Sleep(10)
					Me.setY(Ycoord + 1)
				'    i++;
				'}
				'if (i == 301)
				'{
				'    openDoors();
				'}
			'}
		End Sub
		Public Sub setY(ByVal newY As Integer)
			Ycoord = newY
		End Sub
		Public Sub setX(ByVal newX As Integer)
			Xcoord = newX
		End Sub
		Public Sub updateLiftPosition()
			p.Location = New Point(Xcoord, Ycoord)
		End Sub

		' Opening doors animation 
		Public Sub openDoors()
			Dim animationThread As New Thread(New ThreadStart(AddressOf animateOpening))
			animationThread.Start()
		End Sub
		Private Sub animateOpening()
			If getDoorStatus().Equals("closed") Then
				Dim i As Integer = 0
				setDoors("opening")
				Do While i <= 23
					p.BackgroundImage = doorAnimationImages(i)
					Thread.Sleep(40)
					i += 1
				Loop
				setDoors("open")
			End If
		End Sub

		' Closing doors animation 
		Public Sub closeDoors()
			Dim animationThread As New Thread(New ThreadStart(AddressOf animateClosing))
			animationThread.Start()
		End Sub
		Private Sub animateClosing()
			If getDoorStatus().Equals("open") Then
				Dim i As Integer = 0
				setDoors("closing")
				Do While i <= 23
					p.BackgroundImage = doorCloseAnimationImages(i)
					Thread.Sleep(40)
					i += 1
				Loop
				setDoors("closed")
			End If
		End Sub
	End Class
End Namespace
